<%--
  Created by IntelliJ IDEA.
  User: Mr.G
  Date: 2019/1/9
  Time: 15:12
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <title>Title</title>
    <%@include file="/WEB-INF/jsp/common.jsp"%>
</head>
<body>

<div class="layui-fluid">
    <h2>病例录入管理</h2>
    <hr>
    <div class="layui-row">
        <div class="layui-col-sm4" >
            <table id="bingliluru" lay-filter="bingliluru"  style="margin-top: 38px;"></table>
        </div>
        <div class="layui-col-sm7 layui-col-sm-offset1">
            <button type="button" class="layui-btn" onclick="yiqi()">仪器预约</button>
            <button type="button" class="layui-btn" onclick="kaiyao()">开药</button>
            <button type="button" class="layui-btn" onclick="lishi()">查看历史就诊信息</button>
            <div>
                <form class="layui-form" action="" lay-filter="zhiliaoform" id="zhiliaoform">
                    <table class="layui-table">
                        <tbody>
                        <input type="hidden" id="bianhao" name="caseId"  class="layui-input">
                        <input type="hidden" id="userid" name="userId.userid" :value="userid"  class="layui-input">
                        <tr>
                            <td>病例编号:</td>
                            <td>
                                <select name="memberCaseNo.medicalRecordNo" id="medicalRecordNo" lay-filter="uu" lay-verify="required" lay-search="">
                                    <option value="">直接选择或搜索选择</option>
                                </select>
                            </td>
                            <td>客户编号:</td>
                            <td><input type="text" id="memberId" name="memberId.cardMemberId" class="layui-input"></td>
                        </tr>
                        <tr>
                            <td>治疗开始时间:</td>
                            <td><input type="text" id="treatmentStartTime" name="stateData"  lay-verify="date" placeholder="H点m分" autocomplete="off" class="layui-input"></td>
                            <td>治疗结束时间:</td>
                            <td><input type="text" id="treatmentFinishTime" name="endData" lay-verify="date" placeholder="H点m分" autocomplete="off" class="layui-input"></td>
                        </tr>
                        <tr>
                            <td>治疗状态:</td>
                            <td> <select id="caseStatus" lay-filter="caseStatus" name="caseStatus">
                                <option value="1">正常</option>
                                <option value="0" selected>异常</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td>主诉:</td>
                            <td colspan="5"><textarea name="caseTell" id="treatmentRemark"  lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea></td>
                        </tr>
                        <tr>
                            <td>治疗师检查结果:</td>
                            <td colspan="5"><textarea name="caseTest" id="caseTell"  lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea></td>
                        </tr>
                        <tr>
                            <td>案例分析:</td>
                            <td colspan="5"><textarea name="caseReview" id="caseReview"  lay-verify="required" placeholder="请输入" class="layui-textarea"></textarea></td>
                        </tr>
                        </tbody>
                    </table>
                    <button type="button" class="layui-btn" onclick="Ttianjia()"   id="wancheng" style="display: none">完成治疗</button>
                    <button type="button" class="layui-btn" onclick="Xxiugai()"  id="tijiao">提交</button>


                </form>
            </div>

        </div>
    </div>
</div>

<script>
    layui.config({
        base: '${path}/assets/layui/lay/modules/'
    }).extend({
        notice : 'notice'
    })

    var table;
    var form ;
    var laydate;
    var notice;

    layui.use(['table','form','laydate','notice'],function () {
        table = layui.table;
        form = layui.form;
        laydate = layui.laydate;
        notice = layui.notice;

        //渲染表格
        table.render({
            elem: '#bingliluru',
            url: '${path}/bingliluru/list',
            page:true,
            even: true,
            toolbar:'#toolbarDemo',
            cols:[[
                {type:'radio'},
                {field:'medicalRecordNo',title:'病例编号',width:80, sort: true},
                {field:'reserveName',title:'患者姓名',width:90},
                {field:'shijian',title:'预约时间',templet:function (d) {
                        return d.reserveopentime+" -- "+d.reserveopencole
                    }},
                {field:'reserveType',title:'是否完成',width:90,templet:function (d) {
                        if (d.reserveType == 0){
                            return "否"
                        } else{
                            return "是";
                        }
                    }},
            ]]
        })

        //监听治疗状态框
        form.on('select(caseStatus)', function(data){
            var state = data.value;
            if (state == 0){
                $("#tijiao").show();
                $("#wancheng").hide();
            }else{
                $("#wancheng").show();
                $("#tijiao").hide();
            }
        });

        //监听单选框选中事件
        table.on('radio(bingliluru)',function (obj) {
            $.get("${path}/bingliluru/getCaseInfoById",{memberCaseNo:obj.data.medicalRecordNo},function (getstr) {
                getstr["memberCaseNo.medicalRecordNo"] = getstr.memberCaseNo.medicalRecordNo;
                getstr["memberId.cardMemberId"] = getstr.memberId.cardMemberId;
                getstr["userId.userid"] = getstr.userId.userid;
                form.val('zhiliaoform',getstr);
                form.render("select")
            })

        })

        //取出病例列表
        $.get('${path}/bingliluru/getjiuzhen',function (data) {
            $.each(data,function(){
                var opt = $("<option></option>").appendTo("#medicalRecordNo");
                opt.text(this.medicalRecordNo).val(this.medicalRecordNo);
            });
            layui.form.render();
        });


    })

    //查看历史就诊信息
    function lishi() {
        var rows = table.checkStatus('bingliluru');
        var data = rows.data;
        console.log(data)
        if (data.length == 0){
            layer.msg('请选择病人！');
            return false;
        }
        console.log(data[0].reserveshenfen)

        $.get("${path}/yuyuedengji/returnlishi",{shenfen:data[0].reserveshenfen},function (lishiList) {
            $.get("${path}/bingliluru/lishi",function (str) {
                layer.open({
                    type: 1,
                    title: '历史信息查询',
                    area: '1100px',
                    maxmin: true,
                    content : str,
                    btn:['确定','取消'],
                    success:function(){
                        //历史预约就诊信息
                        table.render({
                            elem: '#lishidengji'
                            ,url:''
                            ,data:lishiList.data
                            ,even : true
                            ,id : "lishidengji"
                            ,cols: [[
                                {field:'caseDate', title: '就诊时间'}
                                ,{field:'memberCaseNo',  title: '预约患者',templet:function (d) {
                                        return d.memberCaseNo.reserveName;
                                    }}
                                ,{field:'caseTell',  title: '就诊项目'}
                                ,{field:'yisheng_name', title: '治疗医生',templet:function (d) {
                                    return d.userId.username;
                                }},{field:'caseTell',title:'案例告诉'},
                                {field:'caseReview',title:'案例分析'},
                                {field:'caseTest',title:'治疗师回复'},
                                {field:'hello',title:'操作',templet:function (d) {
                                        return '<button type="button" class="layui-btn" onclick="lishichaxun('+d.caseId+')"  id="tijiao">查看</button>'
                                    }},
                            ]]
                        });
                    },
                    yes:function (index) {
                        layer.close(index);
                    }
                })
            })
        })



    }


    function lishichaxun(reserveId) {
        console.log(reserveId)
        $.get("${path}/case/getcaseinfo",{reserveId:reserveId},function (data) {
            $.get("${path}/case/xiangqing",function (str) {
                layui.layer.open({
                    type: 1,
                    title: '治疗照片查询',
                    area: '800px',
                    maxmin: true,
                    content : str,
                    btn:['确定','取消'],
                    success:function(){
                        console.log(data)
                        console.log(data.imgpath)
                        if (data.imgpath != null){
                            var img = data.imgpath.split("-")
                            $.each(img,function () {
                                console.log(this)
                                if (this == ''){
                                    return
                                }
                                $img = $("<img class='img' src='${path}/"+this+"' alt=''>");
                                $img.appendTo($("#img"))
                            })
                        }else{
                            $img = $("<h3>暂无照片</h3>");
                            $img.appendTo($("#img"))
                        }


                    },
                    yes:function (index) {
                        layer.close(index);
                    }
                })
            })
        })

    }
    //预约仪器
    function yiqi() {
        var rows = table.checkStatus('bingliluru');
        var data = rows.data;
        console.log(rows);
        $.get("${path}/bingliluru/yuyuezhiliaoyi",function (str) {
            layui.layer.open({
                type: 1,
                title: '预约治疗仪',
                area: '800px',
                maxmin: true,
                content : str,
                btn:['确定','取消'],
                success:function(){
                   if (data.length != 0){
                       setTimeout(function () {
                           data[0]["caseNo"] = data[0].medicalRecordNo;

                           data[0]["memberId"] = data[0].membershipCardNo;
                           data[0]["userId"] = data[0].userId;
                           console.log(data[0].membershipCardNo)
                           console.log(data[0])
                           form.val("yiqifrom",data[0])
                           form.render("select")
                       },300)
                   }

                },
                yes:function (index) {

                    $("#yiqiname").val($('select[name="instrId"] option:selected').text())


                    var parems = $("#yiqifrom").serialize();
                    console.log(parems)

                    $.get("${path}/reser/yuyuezhiliaoyi",parems,function (d) {

                        var caseNo = $("#caseNos").val();

                        $.get("${path}/reser/getByReserveInstr",{caseId:caseNo},function (getstr) {
                            layer.close(index);
                            layui.notice.init({
                                type: "warm",
                                autoClose: true,
                                title: "患者 “"+getstr.reserveName+"” 预约 “"+$('select[name="instrId"] option:selected').text() +"” 成功！"
                            });
                        })

                    })
                }
            })
        })
    }

    //开药
    function kaiyao() {
        $.get("${path}/bingliluru/kaiyao",function (str) {
            var rows = table.checkStatus('bingliluru');
            var data = rows.data;
            layui.layer.open({
                type: 1,
                title: '开药',
                area: '800px',
                maxmin: true,
                content : str,
                btn:['确定','取消'],
                success:function(){
                    if (data.length != 0){
                        setTimeout(function () {
                            data[0]["bingLiNum"] = data[0].medicalRecordNo;

                            data[0]["menberId"] = data[0].membershipCardNo;
                            data[0]["userId"] = data[0].userId;
                            console.log(data[0].membershipCardNo)
                            console.log(data[0])
                            form.val("yiqifrom",data[0])
                            form.render("select")
                        },300)
                    }

                },
                yes:function (index) {

                    /*数量数组*/
                    var vals = new Array();

                    var parems = $("#yiqifrom").serialize();
                    $(".ypnumber").each(function(){
                        vals.push($(this).val());
                        if($(this).val()== ''){
                            flag = false;
                        }
                    });
                    console.log(vals);


                    $.get("${path}/kaiyao/addKaiYao",parems,function (addstr) {
                        var i = 0
                        for(var u of array){
                            u["kyId"] =addstr.msg;
                            u["caseNo"] = addstr.bingLiNum;
                            u["ypNum"] = vals[i];

                            if (u["ypkc"]<u["ypNum"]){
                                layer.msg("“"+u["ypname"]+"”库存不足，请及时进货");
                            }

                            console.log(u)
                            $.post("${path}/kaiyao/addKaiYaoInfo",u,function () {
                            })
                            i++;
                        }

                    })

                    layer.close(index);
                    console.log(array)

                }
            })
        })
    }

    //完成治疗
    function Ttianjia() {
        var parems = $("#zhiliaoform").serialize();
        $.get("${path}/bingliluru/editCaseInfo",parems,function (data) {
            $.get("${path}/bingliluru/editType",{medicalRecordNo:data.medicalRecordNo},function (d) {
                form.val('zhiliaoform',null);

                table.reload("bingliluru")
                layui.notice.init({
                    type: "default",
                    autoClose: true,
                    title: "完成治疗成功！"
                });
            })


        })

    }

    //提交
    function Xxiugai() {
        var parems = $("#zhiliaoform").serialize();
        $.get("${path}/bingliluru/editCaseInfo",parems,function (data) {
                form.val('zhiliaoform',null);

                table.reload("bingliluru")
                layui.notice.init({
                    type: "default",
                    autoClose: true,
                    title: "完成治疗成功！"
                });
        })

    }
</script>

</body>
</html>
